//
//  BottomCollectionVIewCell.m
//  ceshi
//
//  Created by liutaifeng on 16/5/18.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "BottomCollectionViewCell.h"
#import "Common.h"
@interface BottomCollectionViewCell ()
<
 UICollectionViewDataSource,
 UICollectionViewDelegate
>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic ,strong) NSArray *colorArray;

@property (nonatomic, strong) NSMutableArray *inComePicArray;
@property (nonatomic, strong) NSMutableArray *incomeTitleArray;
@property (nonatomic ,strong) NSArray *incomeColorArray;
@end

@implementation BottomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor redColor];
        [self createView];
    }
    return self;
}
- (void)createView {
    self.colorArray = @[@127, @184, @14,
                        @88, @94, @170,
                        @245, @130, @32,
                        @51, @71, @95,
                        @234, @102, @166,
                        @235, @79, @56,
                        @0, @154, @214,
                        @234, @128, @16,
                        @80, @183, @193,
                        @157, @85, @184,
                        @69, @185, @124,
                        @86, @171, @228,
                        @86, @69, @45,
                        @235, @79, @56,
                        @0, @187, @156,
                        @86, @171, @228,
                        @157, @85, @184,
                        @234, @128, @16, @252, @39, @28];
    self.picArray = [NSMutableArray arrayWithObjects:@"yiban.png", @"yongcan.png", @"jiaotong.png", @"fushi.png", @"liren.png", @"riyongpin.png", @"yule.png", @"shicai.png", @"lingshi.png", @"jiushui.png", @"zhufang.png", @"tongxun.png", @"jiaju.png", @"renqing.png", @"xuexi.png", @"yiliao.png", @"lvyou.png", @"shuma.png", @"qita.png", nil];
    self.titleArray = [NSMutableArray arrayWithObjects:@"一般", @"用餐", @"交通", @"服饰", @"丽人", @"日用品", @"娱乐", @"食材", @"零食", @"酒水", @"住房", @"通讯", @"家居", @"人情", @"学习", @"医疗", @"旅游", @"数码", @"其他", nil];
    
    self.inComePicArray = [NSMutableArray arrayWithObjects:@"yiban1.png", @"baoxiao.png", @"gongzi.png", @"hongbao.png", @"jianzhi.png", @"jiangjin.png", @"touzi.png", @"qitashouru.png", nil];
    self.incomeTitleArray = [NSMutableArray arrayWithObjects:@"一般", @"报销", @"工资", @"红包", @"兼职", @"奖金", @"投资", @"其他", nil];
    
    self.incomeColorArray = @[@234, @128, @16, @235, @79, @56, @235, @79, @56, @235, @79, @56, @244, @198, @0, @234, @128, @16, @235, @79, @56, @221, @101, @114];
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(MAIN_WIDTH / 6, MAIN_HEIGHT / 10);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    
    [self.contentView addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[IconCollectionVIewCell class] forCellWithReuseIdentifier:NSStringFromClass([IconCollectionVIewCell class])];
    
}


- (void)layoutSubviews {
    [super layoutSubviews];
    _collectionView.frame = self.contentView.frame;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_isState == 1) {
        if (_isBooL == 0) {
            return 18;
        }
        else {
            return _picArray.count - 18;
        }
    }
    else {
        return 8;
    }
    
}
- (void)setIsState:(BOOL)isState {
    if (_isState != isState) {
        _isState = isState;
        [_collectionView reloadData];
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IconCollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([IconCollectionVIewCell class]) forIndexPath:indexPath];
    if (_isState == 1) {
        if (_isBooL == 0) {
            [cell.picImageView setImage:[UIImage imageNamed:_picArray[indexPath.row]]];
            cell.titleLabel.text = _titleArray[indexPath.row];
        }
        else {
            [cell.picImageView setImage:[UIImage imageNamed:_picArray[indexPath.row + 18]]];
            cell.titleLabel.text = _titleArray[indexPath.row + 18];
        }
    }
    else {
        [cell.picImageView setImage:[UIImage imageNamed:_inComePicArray[indexPath.row]]];
        cell.titleLabel.text = _incomeTitleArray[indexPath.row];
    }
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    IconCollectionVIewCell *cell = (IconCollectionVIewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (_isState == 1) {
        if (![cell.titleLabel.text isEqualToString:@"编辑"]) {
            if (_isBooL == 0) {
                [self.delegate sendPic:_picArray[indexPath.row] title:_titleArray[indexPath.row] array:@[_colorArray[indexPath.row * 3], _colorArray[indexPath.row * 3 + 1], _colorArray[indexPath.row * 3 + 2]] frame:cell.frame];
                
            }
            else {
                [self.delegate sendPic:_picArray[indexPath.row + 18] title:_titleArray[indexPath.row + 18] array:@[_colorArray[(indexPath.row + 18) * 3], _colorArray[(indexPath.row + 18) * 3 + 1], _colorArray[(indexPath.row + 18) * 3 + 2]] frame:cell.frame];
            }
        }
        
    }
    else {
        if (![cell.titleLabel.text isEqualToString:@"编辑"]) {
              [self.delegate sendPic:_inComePicArray[indexPath.row] title:_incomeTitleArray[indexPath.row] array:@[_incomeColorArray[indexPath.row * 3], _incomeColorArray[indexPath.row * 3 + 1], _incomeColorArray[indexPath.row * 3 + 2]] frame:cell.frame];
        }
    }
    

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
